package GUI;

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Window;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.Document;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.Element;

public class InfoWindow extends JDialog implements MouseListener, FocusListener {
	
	private static InfoWindow instance = null;
	private JTextPane jtp = null; 
	private static Rectangle bounds = null;
	
	public InfoWindow(String text, Window owner)
	{
		super(owner, "Info");
		
		add(new JTextArea(text));
		setSize(200, 200);
		setVisible(true);
		addMouseListener(this);
		addFocusListener(this);
		//bounds =
		
	}
	
	public InfoWindow(String text, boolean html, Window owner)
	{
		super(owner, "Info");
		
		if(html)
		{
			this.setUndecorated(true);
			this.setBackground(new Color(0, 0, 0, 0));
			JTextPane jtp = new JTextPane();
			jtp.setEditable(false);
			jtp.setEditorKit(new HTMLEditorKit());
			jtp.setBackground(new Color(0, 0, 0, 220));
			//jtp.setBackground(new Color(0, 0, 0));
			jtp.setForeground(Color.white);
			jtp.setText(text);
			jtp.addMouseListener(this);
			add(jtp);
			//doc.setOuterHTML(new El, "Hello world!");
		}
		else
		{
			add(new JTextArea(text));
		}
		setSize(300, 250);
		setVisible(true);
		
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice[] gs = ge.getScreenDevices();

		// Get size of each screen
		for (int i=0; i<gs.length && i < 1; i++) {
		    DisplayMode dm = gs[i].getDisplayMode();
		    int screenWidth = dm.getWidth();
		    int screenHeight = dm.getHeight();
		    bounds = new Rectangle(0, 0, screenWidth, screenHeight);
		}
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {}
	@Override
	public void mouseEntered(MouseEvent arg0) {}
	@Override
	public void mouseExited(MouseEvent arg0) 
	{
		this.setVisible(false);
		this.dispose();
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		this.setVisible(false);
		this.dispose();
	}
	
	public void setText(String txt)
	{
		if(jtp != null) 
		{
			jtp.setEditorKit(new HTMLEditorKit());
			jtp.setText(txt);
		}
	}
	
	public static void showToolTip(String txt, int x, int y, Window owner)
	{
		//if(instance == null)
		//{
		if(instance != null) { instance.setVisible(false); instance.dispose(); }
			instance = new InfoWindow(txt, true, owner);
			if(bounds != null)
			{
				if(bounds != null)
				{
					if(x < bounds.x) x = bounds.x;
					if(y < bounds.y) y = bounds.y;
					if(x + instance.getWidth() > bounds.width + bounds.x) 
						x = bounds.width - instance.getWidth();
					if(y + instance.getHeight() > bounds.height + bounds.y) 
						y = bounds.height - instance.getHeight();
				}
			}
			System.out.println("Instance position : " + x + ", " + y);
			instance.setLocation(x, y);
		//}
	/*
		else
		{
			instance.setText(txt);
			if(bounds != null)
			{
				if(x < bounds.x) x = bounds.x;
				if(y < bounds.y) y = bounds.y;
				if(x + instance.getWidth() > bounds.width + bounds.x) 
					x = bounds.width - instance.getWidth();
				if(y + instance.getHeight() > bounds.height + bounds.y) 
					y = bounds.height - instance.getHeight();
			}
			instance.setLocation(x, y);
			instance.setVisible(true);
		}
		*/
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {}
	@Override
	public void focusGained(FocusEvent arg0) {
		System.out.println("Focus gained");
	}
	@Override
	public void focusLost(FocusEvent arg0) {
		System.out.println("Focus lost");
		setVisible(false);
	}
	
	public static void setTooltipBounds(Rectangle b)
	{
		bounds = b;
	}
}
